#-*- coding:utf-8 -*-
from os import sys, path
sys.path.insert(0, path.dirname(path.dirname(path.realpath(__file__))))
from dbconnect import DBConnector

import threading
from time import sleep

class SubDBConnector(DBConnector):
	def __init__(self, filename):
		DBConnector.__init__(self, filename)

	def makeDB(self):
		self._conn.execute('create table tmp(word text unique not null)')

dbc = SubDBConnector('plugindb/randomanswer.db')

##reply for mention.
#@param username user screen name (it doesn't contains "@")
#@param text mention text (removed username)
#@return a dictionary which contains
#- text: main text
#- header: header text
#- footer: footer text
def reply(**kwds):
	return {'header': u'sampleReplyer',
			'text': kwds['text'],
			'footer': None,
			'chain': False,
			}

##reply for mention when all plugin's reply method failed.
#@param username user screen name (it doesn't contains "@")
#@param text mention text (removed username)
#@return a dictionary which contains
#- text: main text
#- header: header text
#- footer: footer text
def replyBackup(**kwds):
	return {'header': u'sampleReplyer',
			'text': kwds['text'],
			'footer': None,
			'chain': False,
			}

##threading class for some working.
#(ex: collecting web data for speak)
class SubThread(threading.Thread):
	##just init self
	def __init__(self):
		super(SubThread, self).__init__()

		##@var _stop
		#flag for thread quit
		self._stop = threading.Event()
	
	##when plugin unloaded, this function may called
	#just setup _stop variable
	def quit(self):
		self._stop.set()
	
	##main stuff.
	#when _stop is setted, this function will be done
	def run(self):
		while not self._stop.isSet():
			sleep(1)


##thread starting function.
def start():
	global thread
	thread = SubThread()
	thread.setDaemon(True)
	thread.start()

##thread stopping function.
def stop():
	global thread
	thread.stop()

##thread quit for unload plugin.
def quit():
	global thread
	thread.quit()

##thread join (wait) for thread end.
def join():
	global thread
	thread.join()
